package jeye.client.thread;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import jeye.client.buffer.FrameBuffer;
import jeye.client.util.HttpClientConnector;
import jeye.client.util.LineReader;

public class FrameBufferThread extends HttpClientThread {
	
	private Long eventId;
	
	private RunnablePlayer playerThread;
	
	private boolean playerLaunched = false;
	
	private FrameBuffer buffer = new FrameBuffer();
	
	private Integer screenWidth;
	
	private Integer screenHeight;
	
	private boolean buffering = false;
	
	private int percentComplete = 0;
	
	private int frameSkip = 1;
	
	private Float quality;
	
	public FrameBufferThread(HttpClientConnector connector, FrameBuffer buffer, Long eventId, RunnablePlayer playerThread) {
		super(connector);
		this.buffer = buffer;
		this.eventId = eventId;
		this.playerThread = playerThread;
	}
	
	private long getRemainingTime(int size, int count, long elapsed) {
		return (size - count) * elapsed / count;
	}
	
	public void run() {
		try {
			InputStream is = getServiceClient().getEventFrameSequence(eventId, screenWidth, screenHeight, quality);
			LineReader reader = new LineReader(is);
			String frameCountStr = reader.readLine();
			int frameCount = Integer.parseInt(frameCountStr);
			int count = 0;
			long startTime = System.currentTimeMillis();
			long length = frameCount / 15 * 1000;
			long downloadTime = 0;
			
			percentComplete = 0;
			buffering = true;

			for(int i=0;i<frameCount;i++) {

				String sizeStr = reader.readLine();
				int size = Integer.parseInt(sizeStr);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				long t1 = System.currentTimeMillis(); 
				
				for(int j=0;j<size;j++) {
					int b = reader.read();
					baos.write(b);
				}
				
				downloadTime = System.currentTimeMillis() - t1;
				
				long elapsed = System.currentTimeMillis() - startTime;
				long remaining = getRemainingTime(size, ++count, elapsed);
				
				percentComplete = count * 100 / frameCount;
				
				if(!playerLaunched) {
//					if(remaining < length || downloadTime <= playerThread.getFrameTime()) {
					if(percentComplete > 20) {
						playerLaunched = true;
						System.out.println("PLAYER LAUNCHED");
						new Thread(playerThread).start();
					}
					else {
						playerThread.repaintPlayer();
					}
				}
				
				buffer.addFrame(new ByteArrayInputStream(baos.toByteArray()));
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			buffering = false;
		}
	}
	
	public void setScreenHeight(Integer screenHeight) {
		this.screenHeight = screenHeight;
	}

	public void setScreenWidth(Integer screenWidth) {
		this.screenWidth = screenWidth;
	}

	public boolean isBuffering() {
		return buffering;
	}

	public int getPercentComplete() {
		return percentComplete;
	}

	public int getFrameSkip() {
		return frameSkip;
	}

	public void setFrameSkip(int frameSkip) {
		this.frameSkip = frameSkip;
	}

	public Float getQuality() {
		return quality;
	}

	public void setQuality(Float quality) {
		this.quality = quality;
	}

}
